package com.rc.saas.mini.controller.rc;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.base.MinipBaseController;
import com.rc.saas.mini.common.base.DataTResponse;
import com.rc.saas.mini.mapper.rc.RcAgreementNotificationMapper;
import com.rc.saas.mini.model.rc.RcAgreementNotification;
import com.rc.saas.mini.model.rc.RcAgreementNotificationExample;
import com.rc.saas.mini.service.rc.RcAgreementNotificationServiceImpl;
import com.rc.saas.mini.vo.base.RcPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description
 * @Author
 * @Date: 2022-05-19
 */

@Api(tags = "18.协议")
@Controller
@RequestMapping("/rcAgreementNotification")
public class RcAgreementNotificationController extends MinipBaseController {

    private static Logger logger = LogManager.getLogger(RcAgreementNotificationController.class);

    @Autowired
    private RcAgreementNotificationServiceImpl rcAgreementNotificationServiceImpl;

    @Autowired
    private RcAgreementNotificationMapper rcAgreementNotificationMapper;

    ///**
    // * 增、改
    // */
    //@ApiOperation("增、改 通知公告表（协议表）")
    //@RequestMapping(value = "/saveOrUpdateRcAgreementNotification", method = RequestMethod.POST)
    //@ResponseBody
    //private TResponse saveOrUpdateRcAgreementNotification(@ApiIgnore HttpSession session, RcAgreementNotification rcAgreementNotification) {
    //    LoginInfoVo loginInfo = readLoginInfo(request);
    //    if (rcAgreementNotification.getAgreementNotificationId() == null) {
    //        rcAgreementNotification.setCreatedTime(new Date());
    //        rcAgreementNotification.setCreatedBy(loginInfo.getMemberId() + "");
    //        rcAgreementNotificationServiceImpl.insertSelective(rcAgreementNotification);
    //    } else {
    //        rcAgreementNotification.setLastUpdatedTime(new Date());
    //        rcAgreementNotification.setLastUpdatedBy(loginInfo.getMemberId() + "");
    //        rcAgreementNotificationServiceImpl.updateByPrimaryKeySelective(rcAgreementNotification);
    //    }
    //    return TResponse.SUCCESS();
    //}

    /**
     * 查询
     */
    @ApiOperation("查询单个协议 ByAgreementType")
    @RequestMapping(value = "/open/selectByAgreementType", method = RequestMethod.POST)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "agreementType", paramType = "query", value = "协议类型 0关于我们 1用户隐私  2服务类型 3会员用户协议", required = true),
    })
    private DataTResponse<RcAgreementNotification> selectByAgreementType(HttpServletRequest request, @RequestParam(value = "agreementType", defaultValue = "0") String agreementType) {
        RcAgreementNotification rcAgreementNotification = new RcAgreementNotification();
        rcAgreementNotification.setAgreementType(agreementType);
        rcAgreementNotification.setStatus("0");

        List<RcAgreementNotification> list = rcAgreementNotificationMapper.listByCondition(rcAgreementNotification);
        if (list != null && list.size() > 0) {
            return DataTResponse.SUCCESS_DATA(list.get(0));
        }
        return DataTResponse.FAIL();
    }

    /**
     * 查询
     */
    @ApiOperation("查询协议")
    @RequestMapping(value = "/open/selectByAgreementNotificationId", method = RequestMethod.POST)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "agreementNotificationId", paramType = "query", value = "id", required = false),
            @ApiImplicitParam(name = "agreementTitle", paramType = "query", value = "标题模糊查询", required = false),
    })
    private DataTResponse<RcAgreementNotification> selectByAgreementNotificationId(HttpServletRequest request, Long agreementNotificationId, String agreementTitle) {
        if (agreementNotificationId != null) {
            return DataTResponse.SUCCESS_DATA(rcAgreementNotificationServiceImpl.selectByPrimaryKey(agreementNotificationId));
        }
        if (agreementTitle != null && agreementTitle != "") {
            RcAgreementNotificationExample rcAgreementNotificationExample = new RcAgreementNotificationExample();
            rcAgreementNotificationExample.createCriteria().andAgreementTitleLike("%" + agreementTitle + "%").andStatusEqualTo("0");
            rcAgreementNotificationExample.setOrderByClause("created_time desc");

            return DataTResponse.SUCCESS_DATA(rcAgreementNotificationServiceImpl.selectByExample(rcAgreementNotificationExample));
        }

        return DataTResponse.SUCCESS();
    }
    ///**
    // * 查询列表
    // */
    //@ApiOperation("查询列表 通知公告表（协议表）")
    //@RequestMapping(value = "/listByRcAgreementNotification", method = RequestMethod.POST)
    //@ResponseBody
    //private TResponse listByRcAgreementNotification(@ApiIgnore HttpSession session, RcAgreementNotification rcAgreementNotification) {
    //    List<RcAgreementNotification> list = rcAgreementNotificationMapper.listByCondition(rcAgreementNotification);
    //    return TResponse.SUCCESS_DATA(list);
    //}

    /**
     * 查询列表分页
     */
    @ApiOperation("查询列表分页协议表")
    @RequestMapping(value = "/open/listPageByRcAgreementNotification", method = RequestMethod.POST)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "agreementType", paramType = "query", value = "协议类型 0关于我们 1用户隐私  2服务类型  3会员协议 默认3 null全部", required = true),
    })
    private DataTResponse<PageInfo<RcAgreementNotification>> listPageByRcAgreementNotification(HttpServletRequest request, @RequestParam(value = "agreementType", defaultValue = "3") String agreementType, RcPageVo rcPageVo) {
        //Page
        PageHelper.startPage(rcPageVo.getPageNo(), rcPageVo.getPageSize());
        RcAgreementNotification rcAgreementNotification = new RcAgreementNotification();
        rcAgreementNotification.setAgreementType(agreementType);
        rcAgreementNotification.setStatus("0");
        if (agreementType==null||"".equals(agreementType)) {
            rcAgreementNotification.setAgreementType(null);
        }

        List<RcAgreementNotification> list = rcAgreementNotificationMapper.listByCondition(rcAgreementNotification);
        PageInfo<RcAgreementNotification> pageInfo = new PageInfo<>(list);
        return DataTResponse.SUCCESS_DATA(pageInfo);
    }
}
